<?php
	set_time_limit(0);
	session_start();
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment; filename=plan1.xls');
	header('Pragma: no-cache');
	header('Expires: 0');
	
	include('../connections/banco.inc');

	$sql = $_SESSION['sql'];
	$result = mysql_query($sql);

	for ($i = 0; $i < mysql_num_fields($result); $i++)
	{
		echo mysql_field_name($result, $i) . "\t";
	}

	echo "\n";

    while($row = mysql_fetch_row($result))
    {
        $conteudo = '';
        for($j = 0; $j < mysql_num_fields($result); $j++)
        {
            if(empty($row[$j]))
            {
                $conteudo .= '' . "\t";
            }
            else
            {
	            if ($row[$j] != '')
	            {
	                $conteudo .= $row[$j] . "\t";
	            }
	            else
	            {
	                $conteudo .= '' . "\t";
	            }
            }
        }

        $conteudo .= "\t";
        echo trim($conteudo);
        echo "\n";
    }
?>